import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ChromeOptions
import time
import os
import undetected_chromedriver as uc
# 获取扩展的绝对路径

extension_dir = os.path.abspath('proxy')

def get_proxy():
    url = "https://dps.kdlapi.com/api/getdps/?secret_id=ondbxg8msovv7ury9qbe&signature=zft63syjut8u3og5m9j5wung50mwah5f&num=1&format=text&sep=1"
    proxy_address = requests.get(url)
    return proxy_address.text


# 创建一个临时的扩展文件 (.crx 不是必须的，加载解压的扩展目录即可)
# chrome_options = ChromeOptions()
chrome_options = Options()
proxy_address = get_proxy()
print(f"===============使用代理:{proxy_address}")
proxy_host = proxy_address.split(":")[0]            # proxy ip
proxy_port = proxy_address.split(":")[1]            # proxy port
username = 'd2231274096'
password = 'ela9v3e9'
proxy_url = f"http://{username}:{password}@{proxy_host}:{proxy_port}"
chrome_options.add_argument(f'--proxy-server={proxy_url}')
# chrome_options.add_argument("--disable-blink-features=AutomationControlled")
# chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])

driver_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'

print('xxxxxxx')
driver = uc.Chrome(driver_executable_path=driver_path,options=chrome_options)
driver.set_window_size(1366,768)
# 给扩展一点时间加载和设置代理
time.sleep(2)

try:
    # 验证代理 https://www.ip112.com/  http://httpbin.org/ip
    driver.get('https://www.ip112.com/')
    time.sleep(10)


finally:
    driver.quit()
